package com.artmofang.service.Impl;

import com.artmofang.bo.ErpStudentBO;
import com.artmofang.bo.ErpStudentUpdateBO;
import com.artmofang.common.utils.MD5Utils;
import com.artmofang.mapper.ArtOaCourseMapper;
import com.artmofang.mapper.ArtOaSchoolClassMapper;
import com.artmofang.mapper.ErpStudentMapper;
import com.artmofang.pojo.ArtOaCourse;
import com.artmofang.pojo.ArtOaSchoolClass;
import com.artmofang.pojo.ArtOaStudent;
import com.artmofang.service.EprStudentService;
import com.artmofang.vo.ErpClassJoinStudentAndUsersVO;
import com.artmofang.vo.ErpStudentDownloadVo;
import com.artmofang.vo.ErpStudentJoinCourseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

import com.artmofang.mapper.StudentAndClassMapper;
import com.artmofang.vo.ErpStudentJoinClassVO;

import java.util.Map;

@Service
public class EprStudentServiceImpl implements EprStudentService {

    @Autowired
    ErpStudentMapper erpStudentMapper;

    @Autowired
    ArtOaCourseMapper artOaCourseMapper;

    @Autowired
    ArtOaSchoolClassMapper artOaSchoolClassMapper;

    @Autowired
    StudentAndClassMapper studentAndClassMapper;
    @Override
    public int add(ErpStudentBO erpStudentBO) {

        ArtOaStudent erpStudent = new ArtOaStudent();

        erpStudent.setOs_name(erpStudentBO.getOs_name());
        erpStudent.setOs_address(erpStudentBO.getOs_address());
        erpStudent.setOs_birthday(erpStudentBO.getOs_birthday());
        erpStudent.setOs_course_id(erpStudentBO.getOs_course_id());
        erpStudent.setOs_evaluate(erpStudentBO.getOs_evaluate());
        erpStudent.setOs_job(erpStudentBO.getOs_job());
        erpStudent.setOs_level_of_learning(erpStudentBO.getOs_level_of_learning());
        erpStudent.setOs_phone(erpStudentBO.getOs_phone());
        erpStudent.setOs_parents_identity(erpStudentBO.getOs_parents_identity());
        erpStudent.setOs_school(erpStudentBO.getOs_school());
        erpStudent.setOs_sex(erpStudentBO.getOs_sex());
        erpStudent.setOs_oesid(erpStudentBO.getOs_oesid());
        erpStudent.setOs_source(erpStudentBO.getOs_source());
        erpStudent.setOs_username(erpStudentBO.getOs_phone());
        erpStudent.setOs_create_time((int)(System.currentTimeMillis()/1000));
        erpStudent.setIs_follow("2");
        try {
            erpStudent.setOs_password(MD5Utils.MD5Str("123456"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return erpStudentMapper.insert(erpStudent);
    }

    @Override
    public int update(Integer studentId, ErpStudentUpdateBO erpStudentUpdateBO) {

        ArtOaStudent erpStudent = new ArtOaStudent();

        erpStudent.setOs_id(studentId);
        erpStudent.setOs_name(erpStudentUpdateBO.getOs_name());
        erpStudent.setOs_source(erpStudentUpdateBO.getOs_source());
        erpStudent.setOs_address(erpStudentUpdateBO.getOs_address());
        erpStudent.setOs_birthday(erpStudentUpdateBO.getOs_birthday());
        erpStudent.setOs_course_id(erpStudentUpdateBO.getOs_course_id());
        erpStudent.setOs_evaluate(erpStudentUpdateBO.getOs_evaluate());
        erpStudent.setOs_job(erpStudentUpdateBO.getOs_job());
        erpStudent.setOs_level_of_learning(erpStudentUpdateBO.getOs_level_of_learning());
        erpStudent.setOs_phone(erpStudentUpdateBO.getOs_phone());
        erpStudent.setOs_parents_identity(erpStudentUpdateBO.getOs_parents_identity());
        erpStudent.setOs_school(erpStudentUpdateBO.getOs_school());
        erpStudent.setOs_sex(erpStudentUpdateBO.getOs_sex());

        return erpStudentMapper.updateByPrimaryKeySelective(erpStudent);
    }

    @Override
    public List<ArtOaCourse> queryCourse(Integer oesid) {

        Example example = new Example(ArtOaCourse.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("oc_oesid", oesid);
        return artOaCourseMapper.selectByExample(example);
    }

    @Override
    public List<ArtOaSchoolClass> queryClass(Integer oesid) {

        Example example = new Example(ArtOaSchoolClass.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("osc_oesid", oesid);
        return artOaSchoolClassMapper.selectByExample(example);
    }

    @Override
    //List<ErpStudentJoinClassVO>
    public List<ErpStudentJoinClassVO> queryErpStudents(Map<String, Object> map) {

        return studentAndClassMapper.queryStudentJoinClass(map);
    }

    @Override
    public List<ErpStudentJoinCourseVO> queryStudentJoinCourse(Map<String, Object> map) {
        return studentAndClassMapper.queryStudentJoinCourse(map);
    }

    @Override
    public List<ErpClassJoinStudentAndUsersVO> queryStudentByClassId(Map<String, Object> map) {
        return studentAndClassMapper.queryStudentByClassId(map);
    }

    @Override
    public List<ErpStudentDownloadVo> queryStudentDownloadList(Map<String, Object> map) {
        return studentAndClassMapper.queryStudentDownloadList(map);
    }
}
